package com.yunxi.service.dubboService.impl.finance;

import com.github.pagehelper.PageInfo;
import com.yunxi.bizToBaseData.pojo.VehiclePriceReqVO;
import com.yunxi.finance.pojo.VehiclePriceQueryReqVO;
import com.yunxi.finance.pojo.VehiclePriceQueryRespVO;
import com.yunxi.finance.pojo.VehiclePriceVO;
import com.yunxi.finance.service.VehiclePriceDubboService;
import com.yunxi.model.finance.VehiclePrice;
import com.yunxi.model.finance.VehiclePriceDetail;
import com.yunxi.order.pojo.OrderVehicleVO;
import com.yunxi.service.localService.finance.VehiclePriceDetailService;
import com.yunxi.service.localService.finance.VehiclePriceService;
import com.yunxi.service.localService.finance.impl.VehiclePriceDetailServiceImpl;
import com.yunxi.service.localService.finance.impl.VehiclePriceServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 车辆价格 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-22
 */
public class VehiclePriceDubboServiceImpl implements VehiclePriceDubboService {

    @Autowired
    @Qualifier(value = "vehiclePriceServiceImpl")
    VehiclePriceService vehiclePriceService;

    @Autowired
    @Qualifier(value = "vehiclePriceDetailServiceImpl")
    VehiclePriceDetailService vehiclePriceDetailService;

    @Override
    public VehiclePriceVO getById(String id) throws InvocationTargetException, IllegalAccessException {
    VehiclePrice vehiclePrice = vehiclePriceService.getById(id);
    VehiclePriceVO vehiclePriceVO = new VehiclePriceVO();
        BeanUtils.copyProperties(vehiclePrice,vehiclePriceVO);
        return vehiclePriceVO;
    }

    @Override
    public void saveOrUpdate(VehiclePriceVO vehiclePriceVO) throws InvocationTargetException, IllegalAccessException {
        VehiclePrice vehiclePrice = new VehiclePrice();
        BeanUtils.copyProperties(vehiclePriceVO,vehiclePrice);
        vehiclePriceService.saveOrUpdate(vehiclePrice);

        //特殊折扣的处理，需要更新从表
        saveOrUpdateDetail(vehiclePrice);
    }

    public void saveOrUpdateDetail(VehiclePrice vp){
        if(vp.getPriceTypeCode().equals("ZD03")
                ||vp.getPriceTypeCode().equals("ZD04")){
            //切渠道商

            if(StringUtils.isNotEmpty(vp.getPartnerCode())){
                String pcode[] = vp.getPartnerCode().split(",");

                if(pcode.length>0){
                    for(int i=0;i<pcode.length;i++){
                        VehiclePriceDetail vpd = new VehiclePriceDetail();
                        vpd.setPriceId(vp.getPriceId());
                        vpd.setPartnerCode(pcode[i]);
                        if(vp.getUnitType()==0){
                            vpd.setPercent(vp.getAmount()+"");
                        }else{
                            vpd.setAmount(vp.getAmount());
                        }
                        vpd.setEffectDate(vp.getEffectDate());
                        vpd.setValiDate(vp.getValiDate());
                        vpd.setCreateBy(vp.getCreateBy());
                        vehiclePriceDetailService.saveOrUpdate(vpd);
                    }
                }
            }
            //更新历史状态

            //批量更新

            //切单号
            if(StringUtils.isNotEmpty(vp.getOrderVehicleNos())){
                String pcode[] = vp.getOrderVehicleNos().split(",");

                if(pcode.length>0){
                    for(int i=0;i<pcode.length;i++){
                        VehiclePriceDetail vpd = new VehiclePriceDetail();
                        vpd.setPriceId(vp.getPriceId());
                        vpd.setOrderVehicleNo(pcode[i]);
                        if(vp.getUnitType()==0){
                            vpd.setPercent(vp.getAmount()+"");
                        }else{
                            vpd.setAmount(vp.getAmount());
                        }
                        vpd.setEffectDate(vp.getEffectDate());
                        vpd.setValiDate(vp.getValiDate());
                        vpd.setCreateBy(vp.getCreateBy());
                        vehiclePriceDetailService.saveOrUpdate(vpd);
                    }
                }
            }
            //更新历史状态
            //批量更新
        }
    }

    @Override
    public void removeById(String id) {
    vehiclePriceService.removeById(id);
    }

    @Override
    public PageInfo<VehiclePriceVO> findPageByVO(VehiclePriceVO VehiclePriceVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<VehiclePriceVO> pageInfo = vehiclePriceService.pageByVo(VehiclePriceVO,pageNum,pageSize);

      return pageInfo;

   }

    @Override
    public void removeByListVo(List<VehiclePriceVO> baseVehiclePriceVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        vehiclePriceService.removeByListVo(baseVehiclePriceVOList);
    }

    @Override
    public void check(VehiclePriceVO vehiclePriceVO) {
        vehiclePriceService.check(vehiclePriceVO);
    }

    @Override
    public void check(List<VehiclePriceVO> vehiclePriceVOList) {
        if(vehiclePriceVOList!=null){
            for (int i=0;i<vehiclePriceVOList.size();i++){
                VehiclePriceVO vo = vehiclePriceVOList.get(i);
                vehiclePriceService.check(vo);
            }
        }
    }

    @Override
    public BigDecimal compoutePriceByCondition(VehiclePriceQueryReqVO vo){
        return vehiclePriceService.compoutePriceByCondition(vo);
    }

    @Override
    public PageInfo<VehiclePriceQueryRespVO> queryVehiclePriceResult(VehiclePriceQueryReqVO vo,int pageIndex,int pageSize){
         return vehiclePriceService.queryVehiclePriceResult(vo,pageIndex,pageSize);
    }

    @Override
    public BigDecimal queryPriceByCatalogCode(String catalogCode) {
        return vehiclePriceService.queryPriceByCatalogCode(catalogCode);
    }
}
